/*
 * Copyright 2007 by LongTop Corporation.
 * Softpack ChuangXin Building 15F, XiaMen, FuJian, PRC 361005
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 *
 */
package com.longtop.framework.common;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.longtop.intelliweb.common.Log;

/**
 * 基础数据管理监听器类。此类必须放在web.xml的listener中声明<br>
 * <p/> 在服务器启动时，自动把菜单表、代码表等基础数据装载到内存中，以加快访问速度
 * 
 * @author IntelliWeb Team
 * @version 2.0
 */
public class BaseDataListener implements ServletContextListener {
	private final Log logger = Log.getLogger(getClass());

	/**
	 * 服务器启动时，自动执行方法
	 * 
	 * @param servlet
	 *            event
	 */
	public void contextInitialized(ServletContextEvent servlet) {
		try {
			long l1 = System.currentTimeMillis();
			Constant.SERVERPATH = servlet.getServletContext().getRealPath("/");
			BaseDataManage.init();
			long l2 = System.currentTimeMillis();
			logger.logInfo("Load base data complete. Elapse time(ms):" + (l2 - l1));
		} catch (Exception ex) {
			logger.logError("Load base data failed：" + ex.getMessage());
		}
	}

	/**
	 * 服务器停止时，自动执行方法
	 * 
	 * @param servlet
	 *            event
	 */
	public void contextDestroyed(ServletContextEvent servlet) {
		try {
			BaseDataManage.destroy();
			logger.logInfo("Release base data object successfully!");
		} catch (Exception ex) {
			logger.logError("Relase bese data object failed：" + ex.getMessage());
		}
	}
}
